#include <linSlaveTask.h>
#include <ColorMixingTask.h>
#include <applicationTask.h>

TaskState_t        linsTaskState = TASK_STATE_INIT;
LIN_Device_Frame_t activelinsFrame;

//LIN通信帧
LIN_Device_Frame_t lins_cmd_table[] = {
    [0] = {
        .frame_id = 0x01,       //ModeSelection信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [1] = {
        .frame_id = 0x03,       //WorkModeSelection信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [2] = {
        .frame_id = 0x0A,       //SleepOrWake信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [3] = {
        .frame_id = 0x06,       //Indie3信号
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [4] = {
        .frame_id = 0x3C,
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
    [5] = {
        .frame_id = 0x3D,
        .msg_type = LIN_MSG_TYPE_TX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
};

void LIN_Slave_ISR(LIN_Device_Frame_t *frame)
{
    activelinsFrame = *frame;
    TM_PostTask(TASK_ID_LINS);  //中断接受到LIN帧就处理     LIN1   右靠灰线边
}

void hanldeLinMasterCommands(LIN_Device_Frame_t *frame)
{
    Color_CMD_Frame_t *data = (Color_CMD_Frame_t *)frame->data;        //一个8byte数组指针赋值给data指针，data是一个共用体
    switch(frame->frame_id)
    {
        case 0x3C:
          lins_cmd_table[5].responseValid = true;
          for (uint8_t i=0;i<8;i++)
              lins_cmd_table[5].data[i] = 0xAA+i;
        break;
  
        case 0x06:  //Indie3 ID
          switch(data->Mode)
          {
              case 0x11:       //单体控制
                rgbTable[data->ColorRGB.LEDNum].red = data->ColorRGB.red;
                rgbTable[data->ColorRGB.LEDNum].green = data->ColorRGB.green;
                rgbTable[data->ColorRGB.LEDNum].blue = data->ColorRGB.blue;
                rgbTable[data->ColorRGB.LEDNum].level = data->ColorRGB.level;
                rgbTable[data->ColorRGB.LEDNum].transitionTime = data->ColorRGB.transitionTime;
              break;
              
              case 0x22:        //群体控制
                for(int i = 0;i < 6;i ++)
                {
                  rgbTable[i].red = data->ColorRGB.red;
                  rgbTable[i].green = data->ColorRGB.green;
                  rgbTable[i].blue = data->ColorRGB.blue;
                  rgbTable[i].level = data->ColorRGB.level;
                  rgbTable[i].transitionTime = data->ColorRGB.transitionTime;
                }   
              break;
          }
        break;
        
        case 0x01:
        break;       
        case 0x03:
        break;
        case 0x0A:
        break;
    }
}

void LINS_TaskHandler(void)
{
    switch(linsTaskState){
    case TASK_STATE_INIT:       //初始化部分只执行一次
      LINS_Init();              //LINSlave初始化
      LINS_RegisterLINS_IRQ(LIN_Slave_ISR);     //注册中断处理函数
      LINS_RegisterServices(lins_cmd_table, sizeof(lins_cmd_table)/sizeof(LIN_Device_Frame_t));         //注册lin帧
      linsTaskState = TASK_STATE_ACTIVE;
      break;
    case TASK_STATE_ACTIVE:
      hanldeLinMasterCommands(&activelinsFrame);
      break;   
    }
}